home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as / dist / input-file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-01  |  7.5 KB  |  304 lines

  1. /* input_file.c - Deal with Input Files -
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Confines all details of reading source bytes to this module.
  22.  * All O/S specific crocks should live here.
  23.  * What we lose in "efficiency" we gain in modularity.
  24.  * Note we don't need to #include the "as.h" file. No common coupling!
  25.  */
  26.  
  27. #define NDEBUG        /* JF remove asserts */
  28.  
  29. #ifdef USG
  30. #define index strchr
  31. #define setbuffer(stream, buf, size) setvbuf((stream), (buf), _IOLBF, (size))
  32. #endif
  33.  
  34. #include <stdio.h>
  35. #include <assert.h>
  36. /* #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/file.h>
  39. #include <sys/wait.h> */
  40.  
  41. /* #include "style.h" */
  42. #include "input-file.h"
  43.  
  44. /* This variable is non-zero if the file currently being read should be
  45.    preprocessed by app.  It is zero if the file can be read straight in.
  46.  */
  47. int preprocess = 0;
  48.  
  49. void    as_perror();
  50.  
  51. /*
  52.  * This code opens a file, then delivers BUFFER_SIZE character
  53.  * chunks of the file on demand.
  54.  * BUFFER_SIZE is supposed to be a number chosen for speed.
  55.  * The caller only asks once what BUFFER_SIZE is, and asks before
  56.  * the nature of the input files (if any) is known.
  57.  */
  58.  
  59. #define BUFFER_SIZE (32 * 1024)
  60.  
  61. static char in_buf[BUFFER_SIZE];
  62.  
  63. /*
  64.  * We use static data: the data area is not sharable.
  65.  */
  66.  
  67. FILE *f_in;    /* JF do things the RIGHT way */
  68. /* static JF remove static so app.c can use file_name */
  69. char *    file_name;
  70.  
  71. /* These hooks accomodate most operating systems. */
  72.  
  73. void
  74. input_file_begin ()
  75. {
  76.   /* file_handle = -1; */
  77.   f_in = (FILE *)0;
  78. }
  79.  
  80. void
  81. input_file_end ()
  82. {
  83. }
  84.  
  85. int                /* Return BUFFER_SIZE. */
  86. input_file_buffer_size ()
  87. {
  88.   return (BUFFER_SIZE);
  89. }
  90.  
  91. int
  92. input_file_is_open ()
  93. {
  94.   /* return (file_handle >= 0); */
  95.   return f_in!=(FILE *)0;
  96. }
  97.  
  98. #ifdef DONTDEF        /* JF save old version in case we need it */
  99. void
  100. input_file_open (filename, preprocess, debugging)
  101.      char *    filename;    /* "" means use stdin. Must not be 0. */
  102.      int    preprocess;    /* TRUE if needs app. */
  103.      int    debugging;    /* TRUE if we are debugging assembler. */
  104. {
  105.   assert( filename != 0 );    /* Filename may not be NULL. */
  106.   if (filename [0])
  107.     {                /* We have a file name. Suck it and see. */
  108.       file_handle = open (filename, O_RDONLY, 0);
  109.       file_name = filename;
  110.     }
  111.   else
  112.     {                /* use stdin for the input file. */
  113.       file_handle = fileno (stdin);
  114.       file_name = "{standard input}"; /* For error messages. */
  115.     }
  116.   if (file_handle < 0)
  117.     {
  118.       as_perror ("Can't open source file for input", file_name);
  119.     }
  120.   if ( preprocess )
  121.     {
  122. /*
  123.  * This code was written in haste for a frobbed BSD 4.2.
  124.  * I have a flight to catch: will someone please do proper
  125.  * error checks? - Dean.
  126.  */
  127.       int    pid;
  128.       char temporary_file_name [12];
  129.       int    fd;
  130.       union wait    status;
  131.       char    *mktemp();
  132.  
  133.       (void)strcpy (temporary_file_name, "#appXXXXXX");
  134.       (void)mktemp (temporary_file_name);
  135.       pid = vfork ();
  136.       if (pid == -1)
  137.     {
  138.       as_perror ("Can't fork to run app", file_name);
  139.       _exit (144);
  140.     }
  141.       if (pid == 0)
  142.     {
  143.       (void)dup2 (file_handle, fileno(stdin));
  144.       fd = open (temporary_file_name, O_WRONLY + O_TRUNC + O_CREAT, 0666);
  145.       if (fd == -1)
  146.         {
  147.           (void)write(2,"Can't open temporary\n",21);
  148.           _exit (99);
  149.         }
  150.       (void)dup2 (fd, fileno(stdout));
  151. /* JF for testing #define PREPROCESSOR "/lib/app" */
  152. #define PREPROCESSOR "./app"
  153.       execl (PREPROCESSOR, PREPROCESSOR, 0);
  154.       execl ("app","app",0);
  155.       (void)write(2,"Exec of app failed.  Get help.\n",31);
  156.       (void)unlink(temporary_file_name);
  157.       _exit (11);
  158.     }
  159.       (void)wait (& status);
  160.       if (status.w_status & 0xFF00)        /* JF was 0xF000, was wrong */
  161.     {
  162.       file_handle = -1;
  163.       as_warn( "Can't preprocess file \"%s\", status = %xx", file_name, status.w_status );
  164.     }
  165.       else
  166.     {
  167.       file_handle = open (temporary_file_name, O_RDONLY, 0);
  168.       if ( ! debugging )
  169.         {
  170.           if (unlink(temporary_file_name))
  171.         {
  172.           as_perror ("Can't delete temporary file, continuing", temporary_file_name);
  173.         }
  174.         }
  175.     }
  176.       if (file_handle == -1)
  177.     {
  178.       as_perror ("Can't retrieve temporary file", temporary_file_name);
  179.     }
  180.     }
  181. }
  182. #else
  183.  
  184. void
  185. input_file_open (filename,pre)
  186.      char *    filename;    /* "" means use stdin. Must not be 0. */
  187.      int pre;
  188. {
  189.     int    c;
  190.     char    buf[80];
  191.  
  192.     preprocess = pre;
  193.  
  194.     assert( filename != 0 );    /* Filename may not be NULL. */
  195.     if (filename [0]) {    /* We have a file name. Suck it and see. */
  196.         f_in=fopen(filename,"r");
  197.         file_name=filename;
  198.     } else {            /* use stdin for the input file. */
  199.         f_in = stdin;
  200.         file_name = "{standard input}"; /* For error messages. */
  201.     }
  202.     if (f_in==(FILE *)0) {
  203.         as_perror ("Can't open source file for input", file_name);
  204.         return;
  205.     }
  206. #ifndef VMS
  207.     setbuffer(f_in,in_buf,BUFFER_SIZE);
  208. #endif /* VMS */
  209.     c=getc(f_in);
  210.     if(c=='#') {    /* Begins with comment, may not want to preprocess */
  211.         c=getc(f_in);
  212.         if(c=='N') {
  213.             fgets(buf,80,f_in);
  214.             if(!strcmp(buf,"O_APP\n"))
  215.                 preprocess=0;
  216.             if(!index(buf,'\n'))
  217.                 ungetc('#',f_in);    /* It was longer */
  218.             else
  219.                 ungetc('\n',f_in);
  220.         } else if(c=='\n')
  221.             ungetc('\n',f_in);
  222.         else
  223.             ungetc('#',f_in);
  224.     } else
  225.         ungetc(c,f_in);
  226.  
  227. #ifdef DONTDEF
  228.     if ( preprocess ) {
  229.         char temporary_file_name [17];
  230.         char    *mktemp();
  231.         FILE    *f_out;
  232.  
  233.         (void)strcpy (temporary_file_name, "/tmp/#appXXXXXX");
  234.         (void)mktemp (temporary_file_name);
  235.         f_out=fopen(temporary_file_name,"w+");
  236.         if(f_out==(FILE *)0) {
  237.             as_perror("Can't open temp file");
  238.         }
  239.             /* JF this will have to be moved on any system that
  240.                does not support removal of open files.  */
  241.         (void)unlink(temporary_file_name);/* JF do it NOW */
  242.         do_scrub(f_in,f_out);
  243.         (void)fclose(f_in);    /* All done with it */
  244.         (void)rewind(f_out);
  245.         f_in=f_out;
  246.     }
  247. #endif
  248. }
  249. #endif
  250.  
  251. char *
  252. input_file_give_next_buffer (where)
  253.      char *        where;    /* Where to place 1st character of new buffer. */
  254. {
  255.   char *    return_value;    /* -> Last char of what we read, + 1. */
  256.   register int    size;
  257.  
  258.   if (f_in == (FILE *)0)
  259.       return 0;
  260.       /*
  261.        * fflush (stdin); could be done here if you want to synchronise
  262.        * stdin and stdout, for the case where our input file is stdin.
  263.        * Since the assembler shouldn't do any output to stdout, we
  264.        * don't bother to synch output and input.
  265.        */
  266.   /* size = read (file_handle, where, BUFFER_SIZE); */
  267.   if(preprocess) {
  268.     char *p;
  269.     int n;
  270.     int ch;
  271.     extern FILE *scrub_file;
  272.     int scrub_from_file();
  273.     void scrub_to_file();
  274.     int do_scrub_next_char();
  275.  
  276.     scrub_file=f_in;
  277.     for(p=where,n=BUFFER_SIZE;n;--n) {
  278.         ch=do_scrub_next_char(scrub_from_file,scrub_to_file);
  279.         if(ch==EOF)
  280.             break;
  281.         *p++=ch;
  282.     }
  283.     size=BUFFER_SIZE-n;
  284.   } else
  285.     size= fread(where,sizeof(char),BUFFER_SIZE,f_in);
  286.   if (size < 0)
  287.     {
  288.       as_perror ("Can't read source file: end-of-file faked.", file_name);
  289.       size = 0;
  290.     }
  291.   if (size)
  292.     return_value = where + size;
  293.   else
  294.     {
  295.       if (fclose (f_in))
  296.     as_perror ("Can't close source file -- continuing", file_name);
  297.       f_in = (FILE *)0;
  298.       return_value = 0;
  299.     }
  300.   return (return_value);
  301. }
  302.  
  303. /* end: input_file.c */
  304.